/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2015年3月10日         luwei
 */

package com.coocaa.app.gamecenter.pages.list;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.view.Gravity;
import android.view.View;
import android.widget.AbsListView;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.coocaa.app.gamecenter.framework.ui.MultiImageView;
import com.coocaa.launcher.framework.sidebar.item.Item;
import com.coocaa.launcher.framework.sidebar.item.ItemView;
import com.coocaa.x.framework.utils.LogUtils;
import com.coocaa.x.framework.utils.NativeImageCache;
import com.coocaa.x.framework.utils.NativeImageCache.SmartBitmap;
import com.coocaa.x.uipackage.utils.CoocaaFresco;
import com.coocaa.x.uipackage.widget.CoocaaDraweeView;
import com.facebook.drawee.drawable.ScalingUtils;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.tianci.appstore.R;
import com.tianci.appstore.ui.data.SkyListAppData;
import com.tianci.appstore.ui.widget.AlwaysMarqueeTextView;
import com.tianci.appstore.utils.SkyConstants;

public class GameListItem extends Item {
    private SkyListAppData mAppData = null;

    public GameListItem(Context context) {
        super(context);
        AbsListView.LayoutParams params = new AbsListView.LayoutParams(SkyConstants.Div(474),
                SkyConstants.Div(216));
        mItemView.setLayoutParams(params);
    }

    @Override
    public void setData(Object object) {
        this.mAppData = (SkyListAppData) object;
        ((GameListView) mItemView).updateIcon(mAppData.getIconUrl(), Div(145), Div(145));
//        AppIconCache.addAppIcon(mAppData.getPkgName(), mAppData.getIconUrl());
        ((GameListView) mItemView).updateName(mAppData.getTitle());
        ((GameListView) mItemView).updateDownNum(mAppData.getShowInfo());
        if (mAppData.getControll() != null && mAppData.getControll().size() > 0) {
            JSONObject jb = JSONObject.parseObject(mAppData.getControll().get(0));
            String device = jb.getString("device");
            LogUtils.i("luwei", "the device is " + device);
            String[] devices = device.split(",");
            int len = devices.length;
            if (len > 0) {
                SmartBitmap[] mSmartBitmaps = new SmartBitmap[len];
                SmartBitmap bitmap = null;
                for (int j = 0; j < len; j++) {
                    int id = SkyConstants.getIconId(devices[j]);
                    if (id != R.drawable.pic_nopic) {
                        bitmap = NativeImageCache.getInstance().getBitmapById(mContext.getResources(), id, Div(50), Div(50));
                        mSmartBitmaps[j] = bitmap;
                    }
                }
                ((GameListView) (mItemView)).updateControlIcon(mSmartBitmaps);
            }
        }
        super.setData(object);
    }

    @Override
    public void onItemClick(View v) {
//        if (LiteAppApplication.checkNetworkConnection(TipToast.showNetIsNotConnectedToastRunnable)) {
//            Intent intent = new Intent("com.coocaa.gamecenter.detail");
//            intent.putExtra(DetailActivity.INTENT_APPID_KEY, mAppData.getId());
//            intent.putExtra(DetailActivity.INTENT_PACKAGENAME_KEY, mAppData.getPkgName());
//            mContext.startActivity(intent);
//        }
//        super.onItemClick(v);
    }

    private class GameListView extends ItemView {
        private CoocaaDraweeView mIcon = null;
        private TextView mName = null;
        private TextView mDownNum = null;
        private Drawable mIconDrawable = null;
        private MultiImageView mControlIcon = null;

        public GameListView(Context context) {
            super(context);
            enableFocusable(false);
            addIcon();
            addName();
            addDownNumView();
            addControllPointView();
            setBackgroundResource(R.drawable.game_center_list_item_bg);
        }

        private void addIcon() {
            mIcon = new CoocaaDraweeView(mContext);
            mIcon.setBackgroundResource(R.drawable.game_center_item_bg_shadow);
            LayoutParams params = new LayoutParams(SkyConstants.Div(145), SkyConstants.Div(145), Gravity.CENTER_VERTICAL);
            params.leftMargin = Div(18);
            mIcon.setLayoutParams(params);
            mIcon.getHierarchy().setActualImageScaleType(ScalingUtils.ScaleType.FIT_XY);
            mIcon.getHierarchy().setPlaceholderImage(R.drawable.game_center_icon_default);
            addView(mIcon);
        }

        private void addName() {
            mName = new AlwaysMarqueeTextView(mContext);
            mName.setTextColor(Color.WHITE);
            mName.setTextSize(SkyConstants.Dpi(30));
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(Div(260),
                    LayoutParams.WRAP_CONTENT);
            params.leftMargin = Div(180);
            params.topMargin = Div(35);
            addView(mName, params);
        }

        private void addDownNumView() {
            mDownNum = new AlwaysMarqueeTextView(mContext);
            mDownNum.setTextColor(Color.WHITE);
            mDownNum.setTextSize(SkyConstants.Dpi(22));
            mDownNum.setTextColor(Color.parseColor("#346fcd"));
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(Div(260),
                    LayoutParams.WRAP_CONTENT);
            params.leftMargin = Div(180);
            params.topMargin = Div(86);
            addView(mDownNum, params);
        }

        private void addControllPointView() {
            mControlIcon = new MultiImageView(mContext);
            mControlIcon.setDivide(Div(10));
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(Div(270),
                    LayoutParams.WRAP_CONTENT);
            params.leftMargin = Div(180);
            params.topMargin = Div(140);
            addView(mControlIcon, params);
        }

        public void updateControlIcon(SmartBitmap[] bitmaps) {
            mControlIcon.setImages(bitmaps);
        }

        public void updateName(String name) {
            mName.setText(name);
        }

        public void updateIcon(final Bitmap bitmap) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mIcon.setBackgroundDrawable(new BitmapDrawable(bitmap));
                }
            });
        }

        public void updateIcon(String url, int width, int height) {
            ImageRequest request = ImageRequestBuilder.newBuilderWithSource(Uri.parse(url))
                    .setResizeOptions(new ResizeOptions(width, height))
                    .build();
            DraweeController controller = CoocaaFresco.newDraweeControllerBuilder(mContext)
                    .setOldController(mIcon.getController())
                    .setImageRequest(request)
                    .build();
            mIcon.setController(controller);
        }

        public void updateDownNum(String num) {
            mDownNum.setText(num);
        }

        public void updateDrawable(Drawable drawable) {
            mIconDrawable = drawable;
            mIcon.setBackgroundDrawable(drawable);
        }

        public void recycle() {
            mIcon.setBackgroundDrawable(null);
            if (mIconDrawable != null) {
                mIconDrawable.setCallback(null);
            }
        }

        public void destory() {
            if (mControlIcon != null) {
                mControlIcon.destory();
            }
        }

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            mName.setSelected(hasFocus);
            super.onFocusChange(v, hasFocus);
        }
    }

    @Override
    public void destory() {
        mItemView.destory();
    }

    @Override
    public ItemView createItemView() {
        return new GameListView(mContext);
    }
}
